// ----------------------------------------------------------------------------
// Copyright 2006, Martin D. Flynn
// All rights reserved
// ----------------------------------------------------------------------------
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ----------------------------------------------------------------------------
// Description:
//  This class contains packet parsing exceptions.
// ----------------------------------------------------------------------------
// Change History:
//  2006/03/26  Martin D. Flynn
//      Initial release
// ----------------------------------------------------------------------------
package org.opendmtp.j2me.client.base;

import org.opendmtp.j2me.util.StringTools;

/**
 * Exception when working with packets.
 * @author Martin D. Flynn
 * @author Kiet Huynh
 *
 */
public class PacketParseException extends Exception {
  // ------------------------------------------------------------------------

  private Packet packet = null;
  private int errorCode = 0x0000;
  private byte errorData[] = null;
  private boolean terminate = false;

  // ------------------------------------------------------------------------

  /**
   * Constructor that has 2 parameters. The instance variable errorData is 
   * initialized to null.
   * @param errCode The error code.
   * @param packet The packet to be parsed.
   */
  public PacketParseException(int errCode, Packet packet) {
    this(errCode, packet, null);
  }

  /**
   * Constructor that has 3 parameters.
   * @param errCode The error code.
   * @param packet The packet to be parsed.
   * @param errData The error data.
   */
  public PacketParseException(int errCode, Packet packet, byte errData[]) {
    super();
    this.packet = packet;
    this.errorCode = errCode;
    this.errorData = errData;
  }

  // ------------------------------------------------------------------------

  /**
   * Gets the packet.
   * @return The packet of this class.
   */
  public Packet getPacket() {
    return this.packet;
  }

  /**
   * Sets the value of the Packet.
   * @param pkt The new packet whose value will be assigned to this packet.
   */
  public void setPacket(Packet pkt) {
    this.packet = pkt;
  }

  // ------------------------------------------------------------------------

  /**
   * Gets the error code.
   * @return the error code.
   */
  public int getErrorCode() {
    return this.errorCode;
  }

  /**
   * Gets the error data.
   * @return An array containing error data.
   */
  public byte[] getErrorData() {
    return this.errorData;
  }

  /**
   * Creates a client error packet.
   * @return The error packet.
   */
  public Packet createClientErrorPacket() {
    int errCode = this.getErrorCode();
    Packet cause = this.getPacket();
    byte errData[] = this.getErrorData();
    Packet errPkt = Packet.createClientErrorPacket(errCode, cause);
    if (errData != null) {
      // DO NOT RESET PAYLOAD INDEX!!!
      errPkt.getPayload(false).writeBytes(errData, errData.length);
    }
    return errPkt;
  }

  // ------------------------------------------------------------------------

  /**
   * Sets value of 'terminate' to true.
   */
  public void setTerminate() {
    this.terminate = true;
  }

  /**
   * Checks to see if a session was terminated.
   * @return True is the session was terminated. Otherwise, returns false.
   */
  public boolean terminateSession() {
    return this.terminate;
  }

  // ------------------------------------------------------------------------

  /**
   * Converts the error code from a number to a string.
   * @return The string that represents the error code.
   */
  public String toString() {
    int errCode = this.getErrorCode();
    StringBuffer sb = new StringBuffer();
    sb.append("[");
    sb.append(StringTools.toHexString(errCode, 16));
    sb.append("] ");
    //sb.append(ServerErrors.getErrorDescription(errCode));
    return sb.toString();
  }

  // ------------------------------------------------------------------------

}
